// ActionScript file
      	import flash.events.Event;
      	import flash.net.URLLoader;
      	import flash.net.URLRequest;
      	import jcrweb.as3.LanguageUtil;
      	import jcrweb.as3.FlexUtil;
      	import jcrweb.as3.RequestUtil;
      	
      	import mx.collections.XMLListCollection;
      	import mx.controls.Alert;
      	import mx.managers.PopUpManager;
		
		[Bindable]
		private var treeItemObj:XML;
		[Bindable]
	    private var languageUtil:LanguageUtil;
		[Bindable]
        private var rTreeList:XMLListCollection;
		private var loadingTreeFlag:Boolean = false;
		
		private var returnObj:Object;
		[Bindable]
		[Embed(source='../WebContent/images/attent.gif')]
		private var iconAlert:Class;
		
		private function init():void{
			//init tree
	         rTreeList = new XMLListCollection();
	         var treeInitStr:String = "<node id='0' label='/' path='/'>"
										+ "</node>";
			 var treeInitXml:XML = new XML(treeInitStr);
			 rTreeList.addItem(treeInitXml);	
			 languageUtil = LanguageUtil.getInstance();
		}
		public function setReturnobj(obj:Object):void{
			returnObj = obj;
		}
		private function createTreeEventNotify(event:Event): void{
			if(loadingTreeFlag){
				return;
			}
			loadingTreeFlag = true;
			
			treeItemObj = Tree(event.target).selectedItem as XML;
			//request tree data
			requestTreeData(treeItemObj.@path);
			
		}
		private function requestTreeData(path:String):void{
			var para:String = new String();
			para +="?path="+path;
			para =  encodeURI(para);
			para =  encodeURI(para);
			var url:String = RequestUtil.LOADTREE_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,loadTreeData);
		}
		public function loadTreeData(event:Event):void { 
			var loader:URLLoader = URLLoader(event.target); 
			var resutlSize:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_SIZE);
			if(resutlSize != "0"){
				delete treeItemObj.*;
					var allData:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_DATA)
				for(var j:int=0;j<Number(resutlSize);j++){
					var newnode:XML = new XML(FlexUtil.getTag(allData,new String(j+1)));
					treeItemObj.appendChild(newnode);
				}
				rTreeList.itemUpdated(treeItemObj.*);
				tree1.expandChildrenOf(treeItemObj,true);
			}
			//rTreeList.g.itemUpdated(treeItemObj);
			loadingTreeFlag = false;
		}
		private function closeDialog():void{
			PopUpManager.removePopUp(this);
		}
		private function submit():void{
			if(this.returnObj == null){
				closeDialog();
				return;
			}
			if(this.returnObj is XML){
				var nodeXML:XML = this.returnObj as XML
				nodeXML.permissionDirPath = this.input_perdir.text;
			}
			
			if(this.returnObj is TextInput){
				var nodeTi:TextInput = this.returnObj as TextInput
				nodeTi.text = this.input_perdir.text;
				
			}
			closeDialog();
		}
		public function addDirectory():void{
			if(tree1 == null || tree1.selectedItem == null){
				Alert.show("Please select a directory.","Attention",0,null,null,iconAlert);
				return;
			}
			var nodeXml:XML = tree1.selectedItem as XML;
			var srcPath:String = nodeXml.@path;
			if(srcPath.length > 1 && srcPath.substring(0,1)!="/"){
				srcPath = "/"+srcPath;
			}
			var perdirs:Array = this.input_perdir.text.split(",");
			
			var addFlag:Boolean = true;
			if(this.input_perdir.text!="" && perdirs.length >0){
				for(var i:int;i<perdirs.length;i++){
					var str:String  = perdirs[i];
					if(str == srcPath || srcPath.indexOf(str)!=-1){
						addFlag = false;
						break;
					}
				}
			}
			if(addFlag){
				if(this.input_perdir.text==""){
					this.input_perdir.text = srcPath;
				}else{
					this.input_perdir.text = this.input_perdir.text +","+ srcPath;
				}
			}
			
		}
		private function keyDownEvent(keyCode:uint):void{
			if(keyCode == 27){
				this.closeDialog();
			}
		}